package com.huadao.cart_ccs.experiment.sterilization;

import com.huadao.cart_ccs.common.UtilHelpers;
import com.huadao.cart_ccs.common.scheudle.ScheduleUtil;
import com.huadao.cart_ccs.data.bo.CommandWord;
import com.huadao.cart_ccs.data.bo.CultureTankBO;
import com.huadao.cart_ccs.data.bo.DeviceBO;
import com.huadao.cart_ccs.data.bo.UltravioletLightBO;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.entity.Operation;
import com.huadao.cart_ccs.greendao.gen.OperationDao;
import com.huadao.cart_ccs.modle.CultureTankModle;
import com.huadao.cart_ccs.modle.DeviceModle;
import com.huadao.cart_ccs.modle.UltravioletLightModle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class UltravioletSterilizationPresenter extends IUltravioletSterilizationContract.IUltravioletSterilizationPresenter {

    private UltravioletLightModle ultravioletLightModle;
    private CultureTankModle cultureTankModle;
    private Disposable disposable;
    private OperationDao operationDao;
    private DeviceModle deviceModle;

    public UltravioletSterilizationPresenter(IUltravioletSterilizationContract.IUltravioletSterilizationView baseView) {
        attachView(baseView);
        ultravioletLightModle=new UltravioletLightModle();
        cultureTankModle=new CultureTankModle();
        EventBus.getDefault().register(this);
        operationDao=GreenDaoManager.getInstance().getNoCacheSession().getOperationDao();
        deviceModle=new DeviceModle();
    }

    public void closeLight(){
        ultravioletLightModle.close();
    }

    @Override
    void runUltraviolet() {
        if(disposable!=null){
            disposable.dispose();
        }
        ultravioletLightModle.open();
        Operation operation=new Operation();
        operation.setDateTime(new Date());
        operation.setName(Operation.Operate_Sterilization);
        operation.setData(baseView.getUltravioletTime()+"秒");
        operation.setOperator("系统");
        operationDao.insert(operation);
    }

    @Override
    public void closeChangeAirPumpDelay(int leftChangeAirSecond) {
        ScheduleUtil.getInstance().schedule(new Runnable() {
            @Override
            public void run() {
                cultureTankModle.closeMagneticStirrer();
            }
        },leftChangeAirSecond,TimeUnit.SECONDS);
    }

    @Override
    public void shunDown() {
        deviceModle.shutDown();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UltravioletLightBO event) {
        if (event.getCommondWord() == CommandWord.writeSucc[0]) {
            if (event.getOrder() == UltravioletLightBO.on) {
                baseView.onUltravioletOn();
                disposable=Observable.interval(0,1,TimeUnit.SECONDS)
                        .take(baseView.getUltravioletTime())
                        .subscribeOn(Schedulers.io())
                        .map(new Function<Long, Integer>() {
                            @Override
                            public Integer apply(Long aLong) throws Exception {
                                return baseView.getUltravioletTime()-aLong.intValue()-1;
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Integer>() {
                            @Override
                            public void accept(Integer aInt) throws Exception {
                                baseView.sterilizationCountDown(aInt);
                                if(aInt==0){
                                    ultravioletLightModle.close();
                                }
                            }
                        });
            } else if (event.getOrder() == UltravioletLightBO.off) {
                baseView.onUltravioletOff();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CultureTankBO event) {
        if(event.getCommondWord() == CommandWord.writeSucc[0]){
            if(event.getOrder()==CultureTankBO.stop){
                baseView.onCloseChangeAirPump();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DeviceBO event) {
        if(event.getCommondWord() == CommandWord.writeSucc[0]){
            if(event.getOrder()==DeviceBO.shutdown){
                baseView.showToast("马上关机");
                UtilHelpers.shutDownSystem();
            }
        }
    }
    @Override
    public void detachView() {
        super.detachView();
        if(disposable!=null){
            disposable.dispose();
        }
    }
}
